package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.BuyCompany;
import com.banksoft.cps.bean.CompanyUser;
import com.banksoft.cps.bean.SaleOrder;
import com.banksoft.cps.bean.common.ErrorTip;
import com.banksoft.cps.bean.query.BuyCompanyQuery;
import com.banksoft.cps.bean.warpper.BuyCompanyWarpper;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.service.BuyCompanyService;
import com.banksoft.cps.service.SaleOrderService;
import com.banksoft.cps.util.PageUtils;
import com.banksoft.cps.util.StringUtil;
import com.banksoft.cps.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/1/16.
 */
@Controller
@RequestMapping("/admin/buyCompany/")
public class AdminBuyCompanyController extends ControllerConstants {

    @Autowired
    BuyCompanyService buyCompanyService;
    @Autowired
    SaleOrderService saleOrderService;
    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(BuyCompanyQuery query){
        List<BuyCompanyWarpper> list =  buyCompanyService.listWarpper(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(BuyCompanyQuery query){
        long count =  buyCompanyService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }
    @RequestMapping(value = "listAll",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String listAll(String saleOrderId,HttpServletRequest request){
        SaleOrder saleOrder = saleOrderService.findById(saleOrderId);
        BuyCompanyQuery query = new BuyCompanyQuery();
        query.setOrderBy("f_name");
        query.setCompanyId(saleOrder.getCompanyId());
        List<BuyCompany> deviceList = buyCompanyService.listAll(query);
        return toJSONString(createDataTip(deviceList));
    }
    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        BuyCompanyWarpper bean = buyCompanyService.findWarpperById(id);
        return toJSONString(createDataTip(bean));
    }
    @RequestMapping(value = "listAllForSelect",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String listAllForSelect(HttpServletRequest request){
        BuyCompanyQuery query = new BuyCompanyQuery();
        query.setOrderBy("f_name");
        List<BuyCompany> deviceList = buyCompanyService.listAll(query);
        return toJSONString(createDataTip(deviceList));
    }

    @RequestMapping(value = "audit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String audit(String companyId,String auditStatus,String auditResult, HttpServletRequest request){
        try {
            BuyCompany company =  buyCompanyService.findById(companyId);
            company.setAuditStatus(auditStatus);
            company.setAuditResult(auditResult);
            buyCompanyService.save(company);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "updatePassword", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updatePassword(String id,String password){
        try {
            BuyCompany old = buyCompanyService.findById(id);
            old.setPassword(password);
            buyCompanyService.save(old);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(String ids){
        try {
            if(StringUtil.isNullOrEmpty(ids)){
                return toJSONString(new ErrorTip(1,"请选择要删除的记录"));
            }
            buyCompanyService.delete(ids.split(","));
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
}
